package edu.neu.cs6515.xml.converter.test;

import java.io.IOException;

import junit.framework.Assert;

import org.junit.Test;
import org.xml.sax.SAXException;

import com.thoughtworks.xstream.XStream;

import edu.neu.cs6515.test.XMLBasedTest;
import edu.neu.cs6515.xml.IngeniousColor;
import edu.neu.cs6515.xml.Placement;
import edu.neu.cs6515.xml.Score;
import edu.neu.cs6515.xml.Tile;
import edu.neu.cs6515.xml.converter.PlacementConverter;
import edu.neu.cs6515.xml.converter.ScoreConverter;
import edu.neu.cs6515.xml.converter.TileConverter;

public class testPlacementScoreTileConverters {

    XMLBasedTest xmlTest = new XMLBasedTest();

    @Test
    public void testPlacement() throws SAXException, IOException {
        XStream stream = new XStream();
        stream.registerConverter(new PlacementConverter());
        stream.alias("placement", Placement.class);
        Placement p1 = new Placement(IngeniousColor.RED,
                IngeniousColor.GREEN, 5, 0, 5, 1);
        String xml = stream.toXML(p1);
        xmlTest.xmlEquals("<placement c0=\"RED\" c1=\"GREEN\" " +
                "a0=\"0\" d0=\"5\" a1=\"1\" d1=\"5\"/>",
                xml, true);
        String xmlToUnmarshal = "<placement c0=\"PURPLE\" " +
                "c1=\"ORANGE\" a0=\"1000\" d0=\"5\" a1=\"12\" d1=\"0\"/>";
        Placement p2 = new Placement(IngeniousColor.PURPLE,
                IngeniousColor.ORANGE, 5, 1000, 0, 12);
        Assert.assertTrue(p2.equals(stream.fromXML(xmlToUnmarshal)));
    }

    @Test
    public void testScore() throws SAXException, IOException {
        XStream stream = new XStream();
        stream.registerConverter(new ScoreConverter());
        stream.alias("score", Score.class);
        Score s1 = new Score(18, 0, 1, 0, 0, 0);
        String xmlScore = stream.toXML(s1);
        xmlTest.xmlEquals("<score orange=\"18\" red=\"0\" green=\"1\" " +
                "yellow=\"0\" purple=\"0\" blue=\"0\"/>",
                xmlScore, true);
        String xmlToUnmarshal = "<score orange=\"0\" red=\"280\" green=\"1\" " +
                "yellow=\"0\" purple=\"0\" blue=\"0\"/>";
        Score s2 = new Score(0, 280, 1, 0, 0, 0);
        Assert.assertTrue(s2.equals(stream.fromXML(xmlToUnmarshal)));
    }

    @Test
    public void testTile() throws SAXException, IOException {
        XStream stream = new XStream();
        stream.registerConverter(new TileConverter());
        stream.alias("tile", Tile.class);
        Tile t1 = new Tile(IngeniousColor.ORANGE, IngeniousColor.BLUE);
        String xmlScore = stream.toXML(t1);
        xmlTest.xmlEquals("<tile c0=\"ORANGE\" c1=\"BLUE\"/>",
                xmlScore, true);
        String xmlToUnmarshal = "<tile c0=\"BLUE\" c1=\"YELLOW\"/>";
        Tile t2 = new Tile(IngeniousColor.BLUE, IngeniousColor.YELLOW);
        Assert.assertTrue(t2.equals(stream.fromXML(xmlToUnmarshal)));
    }

}
